package 括号问题;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

// 判断一个字符串是否是有效的括号字符串

/**
 * 括号问题一般采用栈来解决
 * 当前字符是右括号则与栈顶元素对比，如果匹配则出栈
 * 否则入栈
 * 如果完全匹配，那么最后栈一定为空
 */
public class isValid {
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        int n=s.length();
        for(int i=0;i<n;++i){
            char c = s.charAt(i);
            if(c==')'){
                if(!stack.isEmpty()&& stack.peek()=='('){
                    stack.pop();
                    continue;
                }
            } else if(c==']'){
                if(!stack.isEmpty()&&stack.peek()=='['){
                    stack.pop();
                    continue;
                }
            } else if(c=='}'){
                if(!stack.isEmpty()&&stack.peek()=='{'){
                    stack.pop();
                    continue;
                }
            }
            stack.push(c);
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println(isValid("())"));
    }
}
